package com.etooling.pdm.controller;

import com.etooling.framework.jpa.JpaQueryBuilder;
import com.etooling.framework.jpa.Sorts;
import com.etooling.framework.jpa.Specifications;
import com.etooling.framework.svo.ReplyObj;
import com.etooling.framework.utils.WebUtils;
import com.etooling.pdm.entity.Goods;
import com.etooling.pdm.entity.GoodsSpecsPropSetting;
import com.etooling.pdm.repository.GoodsRepository;
import com.etooling.pdm.repository.GoodsSpecsPropSettingRepository;
import com.etooling.pdm.repository.GoodsSpecsPropValuesRepository;
import com.etooling.pdm.service.GoodsSpecsPropSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dan
 * @date 2023/12/25 16:31
 */
@Controller
@EnableAutoConfiguration
@RequestMapping(value = "/admin/pdm/goodsSpecsPropSetting")
public class GoodsSpecsPropSettingController {
    @Autowired
    private GoodsRepository goodsRepository;
    @Autowired
    GoodsSpecsPropSettingRepository goodsSpecsPropSettingRepository;
    @Autowired
    GoodsSpecsPropSettingService goodsSpecsPropSettingService;
    @Autowired
    GoodsSpecsPropValuesRepository goodsSpecsPropValuesRepository;


    @ResponseBody
    @RequestMapping("pageQueryPropList")
    ReplyObj pageQueryPropList(HttpServletRequest request, @RequestParam(defaultValue = "", required = false, value = "goodsId") String goodsId) {
        if (WebUtils.checkStr(goodsId)) {
            JpaQueryBuilder<GoodsSpecsPropSetting> pb = Specifications.and();
            pb.eq("goods.id", goodsId);
            pb.eq("status", GoodsSpecsPropSetting.STATUS_RUNNING);
            Sort sort = Sorts.builder().asc("seq").build();
            return WebUtils.flushSuccess(goodsSpecsPropSettingRepository.findAll(pb.build(), sort));
        } else {
            return WebUtils.flushFailure("没有选择对应产品");
        }
    }


    @ResponseBody
    @RequestMapping("getPropList")
    ReplyObj getPropList(HttpServletRequest request,
                         @RequestParam(defaultValue = "", required = false, value = "goodsId") String goodsId,
                         @RequestParam(defaultValue = "", required = false, value = "goodsSpecsId") String goodsSpecsId) {
        if (WebUtils.checkStr(goodsId)) {
            List<Map<String, Object>> propList = goodsSpecsPropSettingRepository.getPropList(goodsId, goodsSpecsId);
            return WebUtils.flushSuccess(propList);
        } else {
            return WebUtils.flushFailure("没有选择对应产品");
        }
    }


    @ResponseBody
    @RequestMapping("getPropListByGoodsCode")
    ReplyObj getPropListByGoodsCode(HttpServletRequest request, @RequestParam(defaultValue = "", required = false, value = "goodsCode") String goodsCode) {
        if (WebUtils.checkStr(goodsCode)) {
            String companyId = WebUtils.getSessionValue(request, WebUtils.LOGIN_COMPANY_ID_KEY);
            Goods goodsByMaterialCode = goodsRepository.getGoodsByMaterialCode(goodsCode, companyId);
            List<Map<String, Object>> propList = goodsSpecsPropSettingRepository.getPropList(goodsByMaterialCode.getId(), "");
            Map<String, Object> result = new HashMap<>();
            result.put("goods", goodsByMaterialCode);
            result.put("propList", propList);
            return WebUtils.flushSuccess(result);
        } else {
            return WebUtils.flushFailure("没有选择对应产品");
        }
    }


    /**
     * 新增
     */
    @ResponseBody
    @RequestMapping("add")
    ReplyObj add(HttpServletRequest request, @RequestParam(defaultValue = "", required = false, value = "json") String json) {
        return goodsSpecsPropSettingService.add(request, json);
    }


    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("update")
    ReplyObj update(HttpServletRequest request, @RequestParam(defaultValue = "", required = false, value = "json") String json) {
        return goodsSpecsPropSettingService.update(request, json);
    }


    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("del")
    ReplyObj del(HttpServletRequest request, @RequestParam(defaultValue = "", required = false, value = "id") String id) {
        try {
            goodsSpecsPropSettingRepository.deleteById(id);
            return WebUtils.flushSuccess("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return WebUtils.flushFailure("无法删除，此配置已经用于“规格”的创建");
        }
    }


}
